C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 |
您所在的位置:网站首页 › 数组 结构体 链表 › C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 |
指针数组:在一个数组中,如果它的元素全部都是指针类型的数据,那么这个数组称为指针数组。 定义:类型名 *数组名[数组长度]; char *suit[3] = {"first","second","third"}; 指向指针的指针:如果一个变量存放的又是另一个指针变量的地址,则称这个指针变量为指向指针数据的指针变量,又称多级指针,简称为指向指针的指针。 定义:类型标识符 * * 指针变量名; 利用指针变量访问另一个变量就是“间接访问”,在一个指针变量中存放一个目标变量的地址,就是“单级间址”。 对于数组suit,由于数组名本身就表示地址,所以可以直接创建二级指针: char **p; p = suit;#include void main(){ int a[5] = {1,3,5,7,9}; int *num[5],i; int **p; for(i=0;ino, "*") == 0) /*这里不用加取址符号,因为no就表示数组的首地址*/ { free(new1); /*释放最后申请的结点空间*/ break; /*结束for语句*/ } printf("Input the score of the student No.%d: ",count + 1); scanf("%f",&new1->score); count++; /*将新结点插入到链表尾,并设置新的尾指针*/ if(count == 1){ head = new1; /*是第一个结点,置头指针*/ } else tail->next = new1; /*不是第一个结点,将新结点插入到链表尾*/ tail = new1; /*设置新的尾结点*/ } /*置新结点的指针域为空*/ new1->next = NULL; return(head); } /*输出链表*/ void printlist(struct student *head){ struct student *p; p = head; if(head == NULL) { printf("List is empty!!!\n"); } else { while(p!=NULL){ printf("%5s %4.1f\n", p->no,p->score); p = p->next; } } } /*插入链表结点*/ NODE * insert(NODE *head, NODE *new, int i){ NODE *pointer; /*将新结点插入到链表中*/ if(head == NULL){ head = new; new->next = NULL; } else { if(i == 0){ new -> next = head; head = new; } else { pointer = head; /*查找单链表的第i个结点(pointer指向它)*/ for(;pointer != NULL && i > 1; pointer = pointer->next,i--); if(pointer == NULL) printf("Out of the range,can't insert new node!\n"); else { /*一般情况下pointer指向第i个结点*/ new -> next = pointer->next; pointer->next = new; } } } return(head); } /*删除链表*/ NODE * dellist(NODE *head,char no[]){ NODE *front; /*front表示要删除结点的前一个结点*/ NODE *cursor; /*cursor表示当前要删除的结点*/ if(head == NULL) { /*空链表*/ printf("\nList is empty\n"); return(head); } if(strcmp(head->no,no == 0)){ /*要删除的结点是表头结点*/ front = head; head = head->next; free(front); } else { /*非表头结点*/ front = head; cursor = head->next; /*通过循环移动到要删除的结点的位置*/ while(cursor != NULL && strcmp(cursor->no,no) != 0) { front = cursor; cursor = cursor ->next; } if(cursor != NULL){ /*找到需要删除的结点进行删除操作*/ front->next = cursor->next; free(front); } else { printf("%5s has not been found!",*no); } } return(head); } |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |